package com.snpic.appaw.module.insure.controller.app.image;

import com.snpic.appaw.framework.common.pojo.CommonResult;
import com.snpic.appaw.module.insure.controller.app.image.vo.VehicleLicenseStorageUploadReqVO;
import com.snpic.appaw.module.insure.controller.app.image.vo.VehicleLicenseStorageUploadRespVO;
import com.snpic.appaw.module.insure.service.image.VehicleLicenseStorageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.snpic.appaw.module.insure.enums.ErrorCodeConstants.S3_FILE_DELETION_FAILED;

/**
 * @author JiJun
 * @date 2025/9/12 15:58
 * @修改时间 2025/9/12 15:58
 * @description 行驶证影像存储，S3
 */
@Tag(name = "C端 - 行驶证影像存储")
@RestController
@RequestMapping("/insure/vehicle_license_storage")
@RequiredArgsConstructor
@Slf4j
@Validated
public class VehicleLicenseStorageController {

	private final VehicleLicenseStorageService vehicleLicenseStorageService;

	@PostMapping("/upload")
	@Operation(summary = "行驶证正面上传")
	public CommonResult<VehicleLicenseStorageUploadRespVO> upload(
		@RequestParam("file") @NotNull(message = "上传的文件不能为空") @Schema(description = "上传的文件") MultipartFile file
	) {

		VehicleLicenseStorageUploadRespVO respVO = vehicleLicenseStorageService.upload(file);

		log.info("调用返回VehicleLicenseStorageUploadRespVO={}", respVO);
		return CommonResult.success(respVO);
	}

	@DeleteMapping("/delete")
	@Operation(summary = "行驶证正面图片删除")
	public CommonResult<Boolean> delete(
		@RequestParam("filePath")
		@NotBlank(message = "文件存储S3中的路径不能为空")
		@Schema(description = "文件存储S3中的路径")
		String filePath
	) {
		Boolean deleted = vehicleLicenseStorageService.delete(filePath);
		if (deleted) {
			log.info("行驶证图片删除成功，filePath={}", filePath);
			return CommonResult.success(deleted);
		} else {
			log.error("行驶证图片删除失败，filePath={}", filePath);
			return CommonResult.error(S3_FILE_DELETION_FAILED);
		}
	}


	@GetMapping("/getUrl")
	@Operation(summary = "S3存储图片获取预览用URL")
	public CommonResult<String> getUrl(
		@RequestParam("filePath")
		@NotBlank(message = "文件存储S3中的路径不能为空")
		@Schema(description = "文件存储S3中的路径")
		String filePath
	) {
		return CommonResult.success(vehicleLicenseStorageService.getUrl(filePath));
	}
}